package cn.wolfcode.shop.cloud.web.controller;

import cn.wolfcode.shop.cloud.common.BusinessException;
import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.domain.OrderInfo;
import cn.wolfcode.shop.cloud.domain.SeckillOrder;
import cn.wolfcode.shop.cloud.domain.User;
import cn.wolfcode.shop.cloud.mq.MQConstans;
import cn.wolfcode.shop.cloud.mq.OrderMessage;
import cn.wolfcode.shop.cloud.msg.SeckillCodemsg;
import cn.wolfcode.shop.cloud.redis.RedisService;
import cn.wolfcode.shop.cloud.service.ISeckillGoodService;
import cn.wolfcode.shop.cloud.service.ISeckillOrderInfoService;
import cn.wolfcode.shop.cloud.service.ISeckillOrderService;
import cn.wolfcode.shop.cloud.util.SeckillKeyPrefixUtil;
import cn.wolfcode.shop.cloud.util.UserValidAnno;
import cn.wolfcode.shop.cloud.vo.SeckillGoodVO;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@RestController
public class SeckillOrderInfoController {
    @Autowired
    private ISeckillGoodService seckillGoodService;

    @Autowired
    private ISeckillOrderInfoService seckillOrderInfoService;
    @Autowired
    private ISeckillOrderService seckillOrderService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public static final ConcurrentHashMap<Long, Boolean> flag = new ConcurrentHashMap<>();

    @RequestMapping("/{path}/doSeckill")
    public Result doSeckill(@PathVariable String path, Long seckillId, @UserValidAnno User user, String uuid) {

        //判断是否登录,如果用户未登录提醒用户违法操作
        if (user == null) {
            throw new BusinessException(SeckillCodemsg.SECKILL_NOT_LOGIN_ERROR);
        }
        //判断路劲是否合法
        String redisPath = redisService.get(SeckillKeyPrefixUtil.SECKILL_PATH, uuid, String.class);
        if (StringUtils.isEmpty(redisPath) || !redisPath.equals(path)) {
            throw  new BusinessException(SeckillCodemsg.SECKILL_OPT);
        }
        //判断本地表示
        Boolean flag = SeckillOrderInfoController.flag.get(seckillId);
        if (flag != null && flag) {
            throw new BusinessException(SeckillCodemsg.SECKILL_STOCKET_OVER);
        }
        //根据秒杀商品的id,上数据库区查询有没有数据,如果没有提醒违法操作
        SeckillGoodVO seckillGoodVO = seckillGoodService.queryByIdCache(seckillId);
        if (seckillGoodVO == null) {
            throw new BusinessException(SeckillCodemsg.SECKILL_OPT);
        }

        //判断商品的秒杀时间是否开始,如果为开始或者是已经结束了
        Date date = new Date();
        if (date.getTime() - seckillGoodVO.getStartDate().getTime() < 0 || date.getTime() - seckillGoodVO.getEndDate
                ().getTime() > 0) {
            throw new BusinessException(SeckillCodemsg.SECKILL_OPT);
        }

        // SeckillOrder seckillOrder = seckillOrderService.queryByUserIdAndSeckillId(user.getId(), seckillId);
        //判断用户是否重复下单,如果重复下单,提示请不要重复下单
        if (redisService.exists(SeckillKeyPrefixUtil.SECKILL_ORDER, user.getId() + ":" + seckillId)) {
            throw new BusinessException(SeckillCodemsg.SECKILL_REPEAT_ERROR);
        }

        //判断是否有库存,如果没有提醒库存不足
        if (seckillGoodVO.getStockCount() <= 0) {
            throw new BusinessException(SeckillCodemsg.SECKILL_STOCKET_OVER);
        }
        //
        long count = redisService.decr(SeckillKeyPrefixUtil.SECKILL_STOCK_COUNT, seckillId + "");
        if (count < 0) {
            SeckillOrderInfoController.flag.put(seckillId, true);
            throw new BusinessException(SeckillCodemsg.SECKILL_STOCKET_OVER);
        }

        //发送seckillId  UserId
        OrderMessage orderMessage = new OrderMessage();
        orderMessage.setUserId(user.getId());
        orderMessage.setSeckillId(seckillId);
        orderMessage.setUuid(uuid);

        rocketMQTemplate.syncSend(MQConstans.CREATE_ORDER_DEST, orderMessage);
        //秒杀
        //String orderNo = seckillOrderInfoService.doSeckill(user.getId(), seckillId);
        return Result.success("正在抢购中.....");
    }

    @RequestMapping("/getOrderNo")
    public Result getorderNo(String orderNo, @UserValidAnno User user) {
        //判断当前用户有没有登录
        if (user == null) {
            throw new BusinessException(SeckillCodemsg.SECKILL_NOT_LOGIN_ERROR);
        }
        OrderInfo orderInfo = seckillOrderInfoService.queryByorderNo(orderNo);

        if (orderInfo == null) {
            throw new BusinessException(SeckillCodemsg.SECKILL_OPT);
        }

        if (!user.getId().equals(orderInfo.getUserId())) {
            throw new BusinessException(SeckillCodemsg.SECKILL_OPT);
        }
        return Result.success(orderInfo);
    }


}
